package org.musicsweet.service;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.musicsweet.constant.StringConstant;
import org.musicsweet.entity.SysRole;
import org.musicsweet.entity.SysRoleMenuRealtion;
import org.musicsweet.entity.SysUser;
import org.musicsweet.mapper.SysRoleMapper;
import org.musicsweet.mapper.SysRoleMenuRealtionMapper;
import org.musicsweet.result.ResultData;
import org.musicsweet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 系统角色Service
 */
@Slf4j
@Service
public class SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleMenuRealtionMapper sysRoleMenuRealtionMapper;


    /**
     * 根据id查询系统角色
     *
     * @param sysRole
     * @return
     */
    public ResultData<SysRole> getSysRoleById(SysRole sysRole) {
        log.info("根据id查询系统角色:"+ JSON.toJSONString(sysRole));
        if(StringUtils.isEmpty(sysRole.getId())){
            return ResultData.createFail("id不能为空");
        }
        sysRole.setDel(StringConstant.DEL_NO);
        SysRole data = sysRoleMapper.getSysRoleById(sysRole);
        return ResultData.successData("查询成功",data);
    }

    /**
     * 多条件查询系统角色
     *
     * @param sysRole
     * @return
     */
    public ResultData<List<SysRole>> getSysRoleList(SysRole sysRole) {
        log.info("多条件查询系统角色:"+ JSON.toJSONString(sysRole));
        sysRole.setDel(StringConstant.DEL_NO);
        List<SysRole> data = sysRoleMapper.getSysRoleList(sysRole);
        return ResultData.successData("查询成功",data);
    }


    /**
     * 添加系统角色
     * @param sysRole
     * @param token
     * @return
     */
    @Transactional
    public ResultData addSysRole(SysRole sysRole,String token) {
        sysRole.setId(StringUtil.getUUID());
        sysRole.setCreateTime(new Date());
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        sysRole.setCreateUserId(currentUser.getId());
        sysRole.setDel(StringConstant.DEL_NO);
        log.info("添加系统角色:"+ JSON.toJSONString(sysRole));
        int i = sysRoleMapper.addSysRole(sysRole);
        if(i>0){
            //添加菜单角色关系表
            List<SysRoleMenuRealtion> menuRealtions = new ArrayList<>();
            for (String menuId : sysRole.getMenuIds()) {
                SysRoleMenuRealtion sysRoleMenuRealtion = new SysRoleMenuRealtion();
                sysRoleMenuRealtion.setId(StringUtil.getUUID());
                sysRoleMenuRealtion.setMenuId(menuId);
                sysRoleMenuRealtion.setRoleId(sysRole.getId());
                sysRoleMenuRealtion.setCreateTime(new Date());
                sysRoleMenuRealtion.setCreateUserId(currentUser.getId());
                sysRoleMenuRealtion.setDel(StringConstant.DEL_NO);
                menuRealtions.add(sysRoleMenuRealtion);
            }
            int j = sysRoleMenuRealtionMapper.batchInsert(menuRealtions);
            if(j > 0){
                return ResultData.createSuccess(null);
            }else{
                return ResultData.createFail("添加系统角色失败");
            }
        }else{
            return ResultData.createFail("添加系统角色失败");
        }
    }


    /**
     * 修改系统角色
     *
     * @param sysRole
     * @return
     */
    @Transactional
    public ResultData updateSysRole(SysRole sysRole,String token) {
        if(StringUtils.isEmpty(sysRole.getId())){
            return ResultData.createFail("id不能为空");
        }
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        //如何修改角色和菜单关联关系呢? (先把这个角色对应的菜单关联关系,进行删除,然后根据前台传最新的菜单进行添加)

        //批量删除角色菜单关系
        SysRoleMenuRealtion searchSysRoleMenuRealtion = new SysRoleMenuRealtion();
        searchSysRoleMenuRealtion.setDel(StringConstant.DEL_NO);
        searchSysRoleMenuRealtion.setRoleId(sysRole.getId());
        List<SysRoleMenuRealtion> sysRoleMenuRealtionList = sysRoleMenuRealtionMapper.getSysRoleMenuRealtionList(searchSysRoleMenuRealtion);
        sysRoleMenuRealtionMapper.batchDelete(sysRoleMenuRealtionList);

        //添加菜单角色关系表
        List<SysRoleMenuRealtion> menuRealtions = new ArrayList<>();
        for (String menuId : sysRole.getMenuIds()) {
            SysRoleMenuRealtion sysRoleMenuRealtion = new SysRoleMenuRealtion();
            sysRoleMenuRealtion.setId(StringUtil.getUUID());
            sysRoleMenuRealtion.setMenuId(menuId);
            sysRoleMenuRealtion.setRoleId(sysRole.getId());
            sysRoleMenuRealtion.setCreateTime(new Date());
            sysRoleMenuRealtion.setCreateUserId(currentUser.getId());
            sysRoleMenuRealtion.setDel(StringConstant.DEL_NO);
            menuRealtions.add(sysRoleMenuRealtion);
        }
        int j = sysRoleMenuRealtionMapper.batchInsert(menuRealtions);
        sysRole.setUpdateUserId(currentUser.getId());
        sysRole.setUpdateTime(new Date());
        log.info("修改系统角色:"+ JSON.toJSONString(sysRole));
        int i = sysRoleMapper.updateSysRole(sysRole);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("修改系统角色失败");
        }
    }


    /**
     * 删除系统角色
     *
     * @param sysRole
     * @return
     */
    @Transactional
    public ResultData delSysRole(SysRole sysRole) {
        if(StringUtils.isEmpty(sysRole.getId())){
            return ResultData.createFail("id不能为空");
        }
        sysRole.setDel(StringConstant.DEL_OK);
        log.info("删除系统角色:"+ JSON.toJSONString(sysRole));
        int i = sysRoleMapper.delSysRole(sysRole);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("删除系统角色失败");
        }
    }
}
